import sys 
from socket import *
import time 

serverName = '172.21.89.4' #本机
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)

#设置超时 1s

clientSocket.settimeout(1)


for i in range(0, 10):
    sentTime = time.time()
    message = ('Ping %d %s' % (i+1, sendTime)).encode()

    try:
        clientSocket.sendto(message, (serverName,serverPort))
        modifiedMessage, serverAddress = clientSocket.recvfrom(1024)

        rtt = time.time() - sentTime

        print('Sequence %d: Reply from %s   RTT = %3f s' % (i+1, serverName, rtt))

    except Exception as e:
        print('Sequence %d: Request timed out' % (i+1))

  # 客户端只需要发送 需要关闭套接字
clientSocket.close()